package com.licenta.fbApp.client;

import java.util.ArrayList;
import com.licenta.fbApp.shared.Restaurant;
import com.licenta.fbApp.client.LoadableTab;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;

public class FoodPageTab extends Composite implements LoadableTab {
		
	private static int CURRENT = 0;
	private static int STEP = 3;
	private int TOTAL;
	
	private static final String IMG_HEADER = "img/HealthyFoodHeader.png";
	private final VerticalPanel panel = new VerticalPanel();
	private final HorizontalPanel buttons = new HorizontalPanel();
	private final VerticalPanel resultPanel = new VerticalPanel();	
	private final Button loadNext = new Button(" -> ");
	private final Button loadPrev = new Button(" <- ");
	private boolean loaded = false;
	Image image;
	private ArrayList<Restaurant> result = new ArrayList<Restaurant>();
	
	private HTML[] rests = new HTML[STEP];
	
	public FoodPageTab() {	
				
		initWidget(panel);
	    addStyleName("tabContentStyle");
	    image = new Image(IMG_HEADER);
		image.setUrl(IMG_HEADER);
		image.setSize("409px", "92px");
		image.addLoadHandler(new LoadHandler() {
		  
		  public void onLoad(LoadEvent event) {}
		  });
	  }	
	  
	  public void load() {
	    if (loaded) {
	      return;
	    }	    
	    loaded = true;	    
	    panel.add(image);  
	    
	    buttons.add(loadPrev);
	    loadPrev.setTitle("Anterior");
	    loadPrev.addClickHandler(new ClickHandler() {
	        
	        public void onClick(ClickEvent event) {
	          loadNext(false);
	        }
	      });
	    
	    buttons.add(loadNext);
	    loadNext.setTitle("Urmator");
	    loadNext.addClickHandler(new ClickHandler() {
	        
	        public void onClick(ClickEvent event) {
	          loadNext(true);
	        }
	      });
	    
	    panel.add(buttons);
	    panel.add(resultPanel);
	  }
	  
	  public void set(ArrayList<Restaurant> results) {
			this.result = results;
			this.TOTAL = result.size();
			add();
	  }
	  
	 private void add()
	  {			
		  int i;
		  Restaurant s;		  
	        for (i = CURRENT; i < (CURRENT + STEP)%TOTAL; i++) {
	        	s = result.get(i);	        	
	        	rests[i-CURRENT] = new HTML(s.details);
	        	resultPanel.add(rests[i-CURRENT]);
	        	System.out.println(rests[i-CURRENT]);
	        	resultPanel.add(new RatePanel(""));
	        }
	  }
	  
	  private void loadNext(boolean more) 
	  {
		  
		  loadNext.setEnabled(false);
		  loadPrev.setEnabled(false);		  
		  if ( more ) {
			  CURRENT += STEP;
			  CURRENT %= TOTAL;
		  } else {
			  CURRENT -= STEP;
			  if (CURRENT < 0 ) {
				  CURRENT = 0;
			  }
		  }		System.out.println(CURRENT);
		  
		  resultPanel.clear();
		  add();
		  loadNext.setEnabled(true);
		  loadPrev.setEnabled(true);
	  }
}
